package dao

import (
	"AiBus/global"
	"AiBus/model"
	"AiBus/utils"
	"errors"
)

func History(UserID string) ([]model.SearchUser, int64, error) {
	info := []model.SearchUser{}
	var Count int64
	tx := global.App.DB.Where("user_id=?", UserID).Find(&info).Count(&Count)
	if tx.Error != nil {
		return nil, 0, tx.Error
	} else if tx.RowsAffected == 0 {
		// 没有数据
		return nil, 0, errors.New("没有查询到对应的数据")
	} else {
		// 将数据存入redis中
		for _, item := range info {
			err := utils.SetHash(UserID, item.SearchName, item.SearchContent)
			if err != nil {
				global.App.Log.Error(err.Error())
				break
			}
		}
		return info, Count, nil
	}

}
